package com.yuanchu.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanchu.common.utils.PageUtils;
import com.yuanchu.common.utils.Query;
import com.yuanchu.goods.dao.YoshopGoodsServiceRelDao;
import com.yuanchu.goods.entity.YoshopGoodsServiceRelEntity;
import com.yuanchu.goods.service.YoshopGoodsServiceRelService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service("yoshopGoodsServiceRelService")
public class YoshopGoodsServiceRelServiceImpl extends ServiceImpl<YoshopGoodsServiceRelDao, YoshopGoodsServiceRelEntity> implements YoshopGoodsServiceRelService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<YoshopGoodsServiceRelEntity> page = this.page(
                new Query<YoshopGoodsServiceRelEntity>().getPage(params),
                new QueryWrapper<YoshopGoodsServiceRelEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<Integer> getServiceIds(Integer goodsId) {
        List<YoshopGoodsServiceRelEntity> serviceRelEntities = this.list(new QueryWrapper<YoshopGoodsServiceRelEntity>().eq("goods_id", goodsId));
        if(CollectionUtils.isEmpty(serviceRelEntities)){
            return Collections.emptyList();
        }
        List<Integer> serviceIds = new ArrayList<>();
        serviceRelEntities.forEach(serviceRel -> serviceIds.add(serviceRel.getServiceId()));
        return serviceIds;
    }

}